#!/bin/bash
SLEEP_TIME=30
SLEEP_TIME_ALL=3600
MUTEX=0
host_ip=

#获取配置文件IP
get_ips()
{
	host_ip=0

	while read -a ARRPY  #读取文件中每一行
	do
		echo ${ARRPY[@]}
		if ! clockdiff -o1 ${ARRPY[@]} ; then
			echo ${ARRPY[@]} is error
		else
			host_ip=${ARRPY[@]}
			break
		fi
	done < /etc/adjtimex.config

	if [ "$host_ip" == "0" ] ; then
		echo error
		exit 0
	fi
}

get_ips

while [ 1 ]
do
	diff_num_ago=`cat /etc/${host_ip}.adjt`
	if ! clockdiff -o1 ${host_ip} | cut -d ' ' -f 2 > /etc/${host_ip}.adjt ; then
		rm -f /etc/${host_ip}.adjt
		get_ips
		continue
	fi
	diff_num_now=`cat /etc/${host_ip}.adjt`
	if [ "${MUTEX}" -eq "0" ]
	then
		#--sleep ${SLEEP_TIME}
		diff_num_ago=`cat /etc/${host_ip}.adjt`
		clockdiff -o1 $host_ip | cut -d ' ' -f 2 > /etc/${host_ip}.adjt
		diff_num_now=`cat /etc/${host_ip}.adjt`
		MUTEX=1
	fi
	#diff_num=$[(${diff_num_now} - ${diff_num_ago}) * 1000 / ${SLEEP_TIME}]
	diff_num=`expr ${diff_num_now} - ${diff_num_ago}`
	diff_num=`expr $diff_num \* 1000`
	diff_num=`expr $diff_num / ${SLEEP_TIME}`   #us


	freq_num=`adjtimex -p | awk 'NR==3' | cut -f 2 -d ':'`
	tick_num=`adjtimex -p | awk 'NR==10' | cut -f 2 -d ':'`
	tolerance=`adjtimex -p | awk 'NR==9' | cut -f 2 -d ':'`

	######
	my_temp_freq_num=${freq_num}
	######
	
if [ ${diff_num_now} -lt 100 ] && [ ${diff_num_now} -gt -100 ] ; then
	tolerance=`expr ${tolerance} / 1000`
	
	diff_tick_size1=`expr 10000000 / $tick_num` #us
	diff_tick_size2=`expr -10000000 / $tick_num` #us
	if [ $diff_num -gt $diff_tick_size1 ] || [ $diff_num -lt $diff_tick_size2 ] ; then
		diff_tick_num=$[${diff_num} / 1000] #ms
		tick_num=$[${tick_num} + ${diff_tick_num} * ${tick_num} / 10000]
		adjtimex -t ${tick_num}
		sleep ${SLEEP_TIME}
		#continue
	else
		diff_freq_num=$[${diff_num} % ${diff_tick_size1}] #us
		freq_num=$[${freq_num} + ${diff_freq_num} * ${tolerance} * ${tick_num} / 10000]
		
		temp_num_1=$[${tolerance} * 1000]
        	temp_num_2=$[${tolerance} * -1000]
		
		if [ $freq_num -gt $temp_num_1 ] || [ $freq_num -lt $temp_num_2 ] ; then
			tolerance_1000=$[${tolerance} * 1000]
			tick_add_num=$[${freq_num} / ${tolerance_1000}]
			freq_num=$[${freq_num} % ${tolerance_1000}]

			tick_num=$[${tick_num} + ${tick_add_num}]
			adjtimex -t ${tick_num}
		fi
		adjtimex -f ${freq_num}
		sleep ${SLEEP_TIME}
		#continue
	fi
	
else
	######
	my_temp_freq=`expr ${diff_num_now} \* 1000`
	#my_temp_freq=`expr ${my_temp_freq} / ${SLEEP_TIME}`
	my_temp_freq=`expr ${my_temp_freq_num} + ${my_temp_freq}`
	tolerance_n=`expr ${tolerance} \* -1`
	if [ ${my_temp_freq} -gt ${tolerance} ] ; then
		my_temp_freq=${tolerance}
	fi
	if [ ${my_temp_freq} -lt ${tolerance_n} ] ; then
		my_temp_freq=${tolerance_n}
	fi
	adjtimex -f ${my_temp_freq}
	sleep ${SLEEP_TIME}
	######
fi

if [ ${diff_num_now} -lt 100 ] && [ ${diff_num_now} -gt -100 ] ; then

	COUNT=`expr ${COUNT} + 1`

        #通过判断，修改睡眠时间
        if [ ${COUNT} -eq 100 ] ; then
                COUNT=0
                sleep ${SLEEP_TIME_ALL}
                if [ ${SLEEP_TIME} -ge 30 ] ; then
                        SLEEP_TIME=`expr ${SLEEP_TIME} / 2`
                else
                        SLEEP_TIME=15
                fi
                if [ ${SLEEP_TIME_ALL} -ge 3600 ] ; then
                        SLEEP_TIME_ALL=`expr ${SLEEP_TIME_ALL} / 2`
                else
                        SLEEP_TIME_ALL=1800
                fi	
                
								hwclock -w
        fi
        if [ ${diff_num} -eq 0 ] ; then
                COUNT=0
                sleep ${SLEEP_TIME_ALL}
                if [ ${SLEEP_TIME} -le 900 ] ; then
                        SLEEP_TIME=`expr ${SLEEP_TIME} \* 2`
                else
                        SLEEP_TIME=1800
                fi
                if [ ${SLEEP_TIME_ALL} -le 43200 ] ; then
                        SLEEP_TIME_ALL=`expr ${SLEEP_TIME_ALL} \* 2`
                else
                        SLEEP_TIME_ALL=86400
                fi
                
								hwclock -w
        fi
fi
	
done &
